package org.wu.framework.authorization.platform.rpc;

import org.wu.framework.authorization.platform.domain.model.sys.dictionary.data.SysDictionaryData;
import org.wu.framework.authorization.platform.domain.model.sys.dictionary.data.SysDictionaryDataRepository;
import org.wu.framework.lazy.orm.web.plus.stereotype.LazyRpc;
import org.wu.framework.translation.data.dictionary.api.ConvertApi;
import org.wu.framework.web.response.Result;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@LazyRpc
public class SysDictionConvertApiRpc implements ConvertApi {


    private final SysDictionaryDataRepository sysDictionaryDataRepository;

    public SysDictionConvertApiRpc(SysDictionaryDataRepository sysDictionaryDataRepository) {
        this.sysDictionaryDataRepository = sysDictionaryDataRepository;
    }


    /**
     * description 查询多个字典
     *
     * @param convertItems 字典大项
     * @param order        控制 code 和name 的顺序
     * @return Map<String, Map < String, String>> 字典大项
     * @exception/throws
     * @author Jia wei Wu
     * @date 2020/8/6 下午7:54
     */
    @Override
    public Map<String, Map<String, String>> getConvertDataByItems(List<String> convertItems, boolean order) {
        Result<List<SysDictionaryData>> listByDictionaryCode = sysDictionaryDataRepository.findListByDictionaryCode(convertItems);
        if(listByDictionaryCode.isSuccessAndHasData()){
            return listByDictionaryCode
                            .getData()
                            .stream()
                            .collect(
                                    Collectors.groupingBy(SysDictionaryData::getDictionaryCode
                                            ,Collectors.toMap(SysDictionaryData::getCode, SysDictionaryData::getName,(A, B)->A))
                            );

        }
        return new LinkedHashMap<>();
    }
}
